<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

	public function __construct(){
		parent::__construct();
		$this->checklogin();
	}

	//检测是否登录
	public function checklogin(){
		if(session('passid')){
			//已登录
		}else{
			$this->redirect('Login/index');
		}
	}

	/*主页*/
    public function index(){
		$STU = M("Student");
		$list = $STU->order('supdate desc')->limit(7)->select();
		$this->assign('list',$list);
        $this->display('adminlte/content'); 
    }
	
	/*添加学生表单*/
	public function addstu(){
		$this->display('adminlte/pages/forms/addform');
	}
	
	/*处理提交学生*/
	public function addstudent(){
		$STU = M("Student");
		$data = I('post.');
		$data['zserid']=session('id');
		//验证表单
		if(!preg_match("/^1[34578]{1}\d{9}$/",$data['telephone'])){
				$this->error('请正确填写');
		}
		if(!preg_match("/^[\u4E00-\u9FA5]{1,6}$/",$data['name'])){
				$this->error('请正确填写');
		}
		if(!preg_match("/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/",$data['birthday'])){
				$this->error('请正确填写');
		}
		if($data['parentname']==''){
			$this->error('请正确填写');	
		}
		if($data['workplace']==''){
			$this->error('请正确填写');	
		}
		if($data['address']==''){
			$this->error('请正确填写');	
		}
		if($STU->create()){
			//$data['supdate']=date("Y-m-d H-i-s" ,time());
			
			$data['indate']=date("Y-m-d H-i-s" ,time());
			$result = $STU->add($data);
			if($result){
			$this->success('新增成功', __CONTROLLER__/'addstu');
			}else{
				$this->error('新增失败');
			}
		}else{
			$this->error('新增失败');
		}

	}

	
	/*学生列表*/
	public function stulist($i=0){
		$p = 10; //每页显示条数
		$STU = M("Student");
		$TH = M("Teacher");
		if($this->isadmin(session('sfid'))){
			$page['cont'] = $STU->count();//总条数
			$page['list'] =ceil($page['cont']/$p); //页数
			$page['page'] =$i;
			$data = $STU->order('id desc')->limit($i*$p,$p)->select();
			for ($x=0;$x<count($data);$x++){
				$data[$x]['workplace'] = substr($data[$x]['workplace'],0,36);
				$data[$x]['address'] = substr($data[$x]['address'],0,36);
				$datatime1 = date_create($data[$x]['birthday']);
				$datatime2 = date_create();
				$interval = date_diff($datatime1,$datatime2);
				$data[$x]['age'] = $interval->format('%y岁');
				$res = $TH->where('id='.$data[$x]['zserid'])->find();
				$data[$x]['zserid']=$res['name'];
			}
			$this->assign('data',$data);
			$this->assign('page',$page);
			$this->display('adminlte/pages/tables/stulist');
		}else{
			$uid = session('id');
			$page['cont'] = $STU->where('zserid='.$uid)->count();//总条数
			cookie('stucont',$page['cont']);
			$page['list'] =ceil($page['cont']/$p); //页数
			$page['page'] =$i;
			$data = $STU->where('zserid='.$uid)->order('id desc')->limit($i*$p,$p)->select();
			for ($x=0;$x<count($data);$x++){
				$data[$x]['workplace'] = substr($data[$x]['workplace'],0,36);
				$data[$x]['address'] = substr($data[$x]['address'],0,36);
				$datatime1 = date_create($data[$x]['birthday']);
				$datatime2 = date_create();
				$interval = date_diff($datatime1,$datatime2);
				$data[$x]['age'] = $interval->format('%y岁');
				$res = $TH->where('id='.$data[$x]['zserid'])->find();
				$data[$x]['zserid']=$res['name'];
			}
			$this->assign('data',$data);
			$this->assign('page',$page);
			$this->display('adminlte/pages/tables/stulist');
		}
	}

	//年龄统计
	public function agediff($x=0){
		$STU = M("Student");
		$date=date_create();
		$year3 = date_format(date_modify($date,"-3 years"),"Y-m-d");
		$year4 = date_format(date_modify($date,"-1 years"),"Y-m-d");
		$year5 = date_format(date_modify($date,"-1 years"),"Y-m-d");
		$year6 = date_format(date_modify($date,"-1 years"),"Y-m-d");

		switch($x){
		case 1:
			$sap['birthday'] = array('GT',$year3);//小于3岁
			break;
		case 2:
			$sap['birthday'] = array(array('GT',$year4),array('ELT',$year3));//3-4
			break;
		case 3:
			$sap['birthday'] = array(array('GT',$year5),array('ELT',$year4));//4-5
			break;
		case 4:
			$sap['birthday'] = array(array('GT',$year6),array('ELT',$year5));//5-6
			break;
		case 5:
			$sap['birthday'] = array('ELT',$year6);//大于6岁
			break;
		}
		if(!$sap){echo "0";}else{
			$result = $STU->where($sap)->select();
			echo count($result);
		}
		
	}
	
	//老师招生统计
	public function zstj($id=1,$i=0){
		$p = 10; //每页显示条数
		$STU = M("Student");
		$TH = M("Teacher");
		$page['uid'] = $id; // 老师id
		$page['cont'] = $STU->where('zserid='.$page['uid'])->count();//获取总条数
		//cookie('stucont',$page['cont']);
		$page['list'] =ceil($page['cont']/$p); //页数
		$page['page'] =$i; //第几页
		$data = $STU->where('zserid='.$page['uid'])->order('id desc')->limit($i*$p,$p)->select();
		for ($x=0;$x<count($data);$x++){
			$data[$x]['workplace'] = substr($data[$x]['workplace'],0,36);
			$data[$x]['address'] = substr($data[$x]['address'],0,36);
			$res = $TH->where('id='.$data[$x]['zserid'])->find();
			$data[$x]['zserid']=$res['name'];
		}
		$this->assign('data',$data);
		$this->assign('page',$page);
		$this->display('adminlte/pages/tables/thzslist');
	}
	/*查找学生表单*/
	public function searchstu(){
		$this->display('adminlte/pages/forms/searchstu');
	}

	/*处理查找学生*/
	public function viewstu(){
		$STU = M("Student");
		$TH = M("Teacher");
		$post = I('post.');
		if($post['sname']){
			$data['name']=$post['sname'];
		}
		if($post['parentname']){
			$data['parentname']=$post['parentname'];
		}
		if($post['telephone']){
			$data['telephone']=$post['telephone'];
		}
		if(!$this->isadmin(session('sfid'))){
			$data['zserid']=session('id');
		}
		
		if($data){
			$result = $STU->where($data)->select();
			for ($x=0;$x<count($result);$x++){
				$result[$x]['address'] = substr($result[$x]['address'],0,36);
				$res = $TH->where('id='.$result[$x]['zserid'])->find();
				$result[$x]['zserid']=$res['name'];
				$datatime1 = date_create($result[$x]['birthday']);
				$datatime2 = date_create();
				$interval = date_diff($datatime1,$datatime2);
				$result[$x]['age'] = $interval->format('%y岁');
			}

			
			$this->assign('data',$result);
			$this->assign('page',count($result));
				
		}
		$this->display('adminlte/pages/tables/searstulist');
		
	}

	//修改学生
	public function editstu(){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		$ST = M("Student");
		if(I('path.2')){
			$data['id']=I('path.2');
		}else{
			$this->error("非法请求");
		}
		$result = $ST->where($data)->find();
		$this->assign('data',$result);
		$this->display('adminlte/pages/forms/editstu');
	}

	//处理修改学生
	public function updatestu(){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		$ST = M("Student");
		$data['id']=I('post.id');
		$data['name']=I('post.name');
		$data['sex']=I('post.sex');
		$data['birthday']=I('post.birthday');
		$data['class']=I('post.class');
		$data['address']=I('post.address');
		$data['parentname']=I('post.parentname');
		$data['telephone']=I('post.telephone');
		$data['workplace']=I('post.workplace');
		$res = $ST->where('id='.$data['id'])->save($data);
		if($res){
			$this->success('修改成功');
		}else{
			$this->error("修改失败");
		}
	}

	//删除学生
	public function dellstu(){
		$this->error("无此权限");
	}

	/*添加老师表单*/
	public function addth(){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		$this->display('adminlte/pages/forms/addth');
	}
	
	/*处理添加老师*/
	public function addther(){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		$Ther = M("Teacher");
		$data = I('post.');
		$data['password'] = md5(I('post.password'));
		$result = $Ther->add($data);
		if($result){
			$this->success('新增成功', __CONTROLLER__/'addth');
		}else{
			$this->error('新增失败');
		}
	}

	
	/*老师列表*/
	public function thlist($i=0){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		$p = 10; //每页显示条数
		$Ther = M("Teacher");
		$page['cont'] = $Ther->count();//总条数
		$page['list'] =ceil($page['cont']/$p); //页数
		$page['page'] =$i;
		$data = $Ther->limit($i*$p,$p)->select();
		$this->assign('data',$data);
		$this->assign('page',$page);
		$this->display('adminlte/pages/tables/thlist');
	}

	/*老师个人信息*/
	public function profile(){
		$this->display('adminlte/profile');
	}

	/*修改老师表单*/
	public function editth(){
		$Ther = M("Teacher");
		if(I('path.2')){
			$data['id']=I('path.2');
		}else{
			$this->error("非法请求");
		}
		$result = $Ther->where($data)->find();
		$this->assign('data',$result);
		$this->display('adminlte/pages/forms/editth');
	}

	/*修改老师信息*/
	public function updateth(){
		if(!$this->isadmin(session('sfid'))){
			$this->error("您无此权限");
		}
		if(I('post.id')){
		$data['id']=I('post.id');
		}
		if(I('post.name')){
			$data['name']=I('post.name');
		}
		if(I('post.password')){
			$data['password']=md5(I('post.password'));
		}
		if(I('post.telephone')){
			$data['telephone']=I('post.telephone');
		}
		if(I('post.state')){
			$data['state']=I('post.state');
		}
		$Ther = M("Teacher");
		$result = $Ther->save($data);
		if($result){
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
		
		
	}
	
	//修改个人信息表单
	public function editpw(){
		$Ther = M("Teacher");
		$thid = session('id');
		$result = $Ther->where('id='.$thid)->find();
		$this->assign('data',$result);
		$this->display('adminlte/pages/forms/editthself');
	}

	//修理老师修改个人信息
	public function editthself(){
		$Ther = M("Teacher");
		$data['id']=session('id');
		$pwago = I('post.passwordago');
		$respw = $Ther->where('id='.$data['id'])->find();
		if(md5($pwago) != $respw['password']){
			$this->error("原密码不正确");
		}
		if(I('post.password')){
			$data['password']=md5(I('post.password'));
		}
		if($pwago == I('post.password')){
			$this->error("新密码不能与旧密码相同");	
		}
		if(I('post.name')){
			$data['name']=I('post.name');
		}
		if(I('post.telephone')){
			$data['telephone']=I('post.telephone');
		}
		if(I('post.notes')){
			$data['notes']=I('post.notes');
		}
		
		$result = $Ther->where('id='.$data['id'])->save($data);
		if($result){
			cookie('name',$data['name']);
			cookie('telephone',$data['telephone']);
			cookie('notes',$data['notes']);
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
	}

	/*管理员权限判断*/
	public function isadmin($id){
		if($id==1){
			return true;
		}else{
			return false;
		}
	}










}